package com.yogee.topseller.activity.my;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.bean.UpdateInfoBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class NameActivity extends HttpActivity {

    @BindView(R.id.name_et)
    EditText nameEt;
    @BindView(R.id.name_title_tv)
    TextView nameTitleTv;
    @BindView(R.id.name_delete_iv)
    ImageView nameDeleteIv;

    private String type;//1.真实姓名 2.电子邮箱 3.工作单位 4.手机号码

    @Override
    protected int getLayoutId() {
        return R.layout.activity_name;
    }

    @Override
    protected void initView() {
        type = getIntent().getStringExtra("type");
        if (type.equals("1")) {
            nameTitleTv.setText("真实姓名");
            nameEt.setHint("请输入真实姓名");
            AppUtil.setLengthFilter(nameEt, 8);
        } else if (type.equals("2")) {
            nameTitleTv.setText("E-mail");
            nameEt.setHint("请输入电子邮箱");
        } else if (type.equals("3")) {
            nameTitleTv.setText("工作单位");
            nameEt.setHint("请输入工作单位");
        } else {
            nameTitleTv.setText("手机号码");
            nameEt.setHint("请输入11位手机号码");
        }
    }

    @OnClick({R.id.name_delete_iv, R.id.name_save_tv, R.id.name_back_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.name_delete_iv:
                nameEt.setText(null);
                break;
            case R.id.name_save_tv://保存
                if (AppUtil.isFastDoubleClick(500)) {
                    return;
                }
                String text = nameEt.getText().toString();
                if (!text.isEmpty()) {
                    getString(text);
                } else {
                    ToastUtils.showToast(this, "输入不能为空");
                }
                break;
            case R.id.name_back_iv://返回
                finish();
                break;
        }
    }

    private void getString(String text) {
        String userid = AppUtil.getUserId(NameActivity.this);
        switch (type) {
            case "1"://姓名
                updateInfo(userid, text, "", "", "");
                break;
            case "2"://电子邮箱
                if (AppUtil.isEmail(text)) {
                    updateInfo(userid, "", "", text, "");
                } else {
                    ToastUtils.showToast(this, "您输入的email格式不正确");
                }
                break;
            case "3"://工作单位
                updateInfo(userid, "", "", "", text);
                break;
            case "4"://手机号
                if (AppUtil.validatePhone(text)) {
                    updateInfo(userid, "", text, "", "");
                } else {
                    ToastUtils.showToast(this, "您输入的手机号格式不正确");
                }
                break;
        }
    }

    private void updateInfo(String userid, String name, String phone, String email, String clo) {
        HttpManager.getInstance().updateInfo(userid, name, phone, email, clo).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<UpdateInfoBean>() {
            @Override
            public void onNext(UpdateInfoBean o) {
                if (o.getResult().equals("1")) {
                    ToastUtils.showToast(NameActivity.this, "修改成功");
                    finish();
                }
            }
        }, this));

    }

}
